﻿using ProjectScheduling.daos;

namespace ProjectScheduling
{
    public partial class UserPage : UserControl
    {
        public static readonly UserPage b = new();
        public UserPage()
        {
            InitializeComponent();
            view_data.AutoGenerateColumns = false;
            Search();
            AddStaffForm.b.action = Search;
        }

        private void Btn_add_Click(object sender, EventArgs e)
        {
            AddStaffForm.b.InitData(new models.Staff());
            AddStaffForm.b.ShowDialog();
        }
        public void Search()
        {
            var table = UsersDao.Base.ToTable("SELECT * FROM Staff WHERE @key='' OR TrueName LIKE CONCAT('%',@key,'%')", ("@key", txt_search_key.Text ?? ""));
            view_data.DataSource = table;
        }

        private void Btn_search_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void View_data_SelectionChanged(object sender, EventArgs e)
        {
            view_data.ClearSelection();
        }

        private void View_data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            var id = int.Parse(view_data.Rows[e.RowIndex].Cells[0].Value.ToString() ?? "0");
            if (e.ColumnIndex == 5)
            {
                MessageBox.Show("登录信息");
                return;
            }
            if (e.ColumnIndex == 6)
            {
                AddStaffForm.b.FindById(id);
                AddStaffForm.b.ShowDialog();

            }
            if (e.ColumnIndex == 7)
            {
                if (MessageBox.Show("确认要删除当前员工信息吗？", "删除确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    StaffDao.Base.Delete(id);
                    Search();
                }
            }
        }
    }
}
